/* $Header: det_diag.c,v 2.2 01/10/02 10:26:09 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  det_diag.c                                            */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To define the detector diagnostic routines.        */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <status.h>
#include <error.h>
#include <diag.h>
#include <p_status.h>
#include <det_ui.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

UI_ERR SetDiagDetInput( POSITION position, U_INT32 bit, ON_OFF on_off )
{
   UI_ERR return_code;

   return_code = SetDiagMode( ON );

   if ( return_code != OK )
   {
      return( return_code );
   }

   if ( bit > 3 )  /* U_INT32 type makes "bit < 0" test unnecessary */
   {
      return INVALID_PARAM;
   }

   /*
   ** the first time only in each session of diagnostic mode,
   ** initialize the diagnostic register image with the current
   ** hardware contents and enable Det_Diag_Mode
   */
   if (!Det_Diag_Mode)
   {
        DiscreteOut2_Diag = DiscreteOut2_Image;
        Det_Diag_Mode = TRUE;
   }

   switch( bit )
   {
      case 0:
         SetDetDiagInput0( position, on_off );
         break;
      case 1:
         SetDetDiagInput1( position, on_off );
         break;
      case 2:
         SetDetDiagInput2( position, on_off );
         break;
      case 3:
         SetDetDiagInput3( position, on_off );
         break;
   }

   return OK;
}

UI_ERR SetDiagDetLinearMode( POSITION position, ON_OFF on_off )
{
   /* Set the detector type used by the signal DSP to linear mode or its      */
   /* "normal" operating mode.  OFF = Normal, ON = Linear                     */

   UI_ERR return_code;

   if ( on_off == ON )
   {
      /* REVISIT: let Mahmoud test his LDID boards using sequences */
      if ( GetDetectorType( position ) != LDID )
      {
         return_code = SetDiagMode( ON );

         if ( return_code != OK )
         {
            return( return_code );
         }
      }
   }

   return( SetDetDiagLinearMode( position, on_off ) );

}
